package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.UserDailySendgiftRecordDao;
import com.jbp.model.Gift;
import com.jbp.model.Lottery;
import com.jbp.model.LuckRank;
import com.jbp.model.UserDailySendgiftRecord;
import com.jbp.service.UserDailySendgiftRecordService;
import com.jbp.utils.StrToMillsUtil;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 每日送礼统计(UserDailySendgiftRecord)表服务实现类
 * @author cc
 * @since 2021-07-22 11:59:17
 */
@Service
public class UserDailySendgiftRecordServiceImpl extends ServiceImpl<UserDailySendgiftRecordDao, UserDailySendgiftRecord> implements UserDailySendgiftRecordService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<UserDailySendgiftRecord> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<UserDailySendgiftRecord> page = this.page(
                new Query<UserDailySendgiftRecord>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateSendGiftRecord(Integer fromUserId, Integer toUserId, Integer coin, Integer roomId, String type) {
        QueryWrapper<UserDailySendgiftRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_user_id", fromUserId);
        queryWrapper.eq("to_user_id", toUserId);
        queryWrapper.eq("room_id", roomId);
        queryWrapper.eq("type", type);
        String date = StrToMillsUtil.millsToStrDate(new Date(), null);
        queryWrapper.eq("send_date", date);
        UserDailySendgiftRecord record = getOne(queryWrapper);
        if (record==null) {
            record = new UserDailySendgiftRecord();
            record.setFromUserId(fromUserId);
            record.setToUserId(toUserId);
            record.setCoin(coin);
            record.setRoomId(roomId);
            record.setSendDate(new Date());
            record.setType(type);
            save(record);
        } else {
            record.setCoin(record.getCoin()+coin);
            updateById(record);
        }
    }

    @Override
    public BigDecimal getAllSend(int userId) {
        return getBaseMapper().allSend(userId);
    }
}